home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / DefaultEditorKit.java < prev    next >
Text File  |  1998-06-30  |  60KB  |  1,823 lines

  1. /*
  2.  * @(#)DefaultEditorKit.java    1.28 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text;
  21.  
  22. import java.io.*;
  23. import java.awt.*;
  24. import java.awt.event.ActionEvent;
  25. import java.text.*;
  26. import com.sun.java.swing.Action;
  27. import com.sun.java.swing.KeyStroke;
  28.  
  29. /**
  30.  * This is the set of things needed by a text component
  31.  * to be a reasonably functioning editor for some <em>type</em>
  32.  * of text document.  This implementation provides a default
  33.  * implementation which treats text as plain text and 
  34.  * provides a minimal set of actions for a simple editor.
  35.  *
  36.  * @author  Timothy Prinzing
  37.  * @version 1.28 04/09/98
  38.  */
  39. public class DefaultEditorKit extends EditorKit {
  40.     
  41.     /**
  42.      * Creates a copy of the editor kit.  This
  43.      * allows an implementation to serve as a prototype
  44.      * for others, so that they can be quickly created.
  45.      *
  46.      * @return the copy
  47.      */
  48.     public Object clone() {
  49.         return new DefaultEditorKit();
  50.     }
  51.  
  52.     /**
  53.      * Gets the MIME type of the data that this
  54.      * kit represents support for.  The default
  55.      * is <code>text/plain</code>.
  56.      *
  57.      * @return the type
  58.      */
  59.     public String getContentType() {
  60.         return "text/plain";
  61.     }
  62.  
  63.     /**
  64.      * Fetches a factory that is suitable for producing 
  65.      * views of any models that are produced by this
  66.      * kit.  The default is to have the UI produce the
  67.      * factory, so this method has no implementation.
  68.      *
  69.      * @return the view factory
  70.      */
  71.     public ViewFactory getViewFactory() {
  72.         return null;
  73.     }
  74.  
  75.     /**
  76.      * Fetches the set of commands that can be used
  77.      * on a text component that is using a model and
  78.      * view produced by this kit.
  79.      *
  80.      * @return the command list
  81.      */ 
  82.     public Action[] getActions() {
  83.         return defaultActions;
  84.     }
  85.  
  86.     /**
  87.      * Fetches a caret that can navigate through views
  88.      * produced by the associated ViewFactory.
  89.      *
  90.      * @return the caret
  91.      */
  92.     public Caret createCaret() {
  93.         return null;
  94.     }
  95.  
  96.     /**
  97.      * Creates an uninitialized text storage model (PlainDocument)
  98.      * that is appropriate for this type of editor.
  99.      *
  100.      * @return the model
  101.      */
  102.     public Document createDefaultDocument() {
  103.         return new PlainDocument();
  104.     }
  105.  
  106.     /**
  107.      * Inserts content from the given stream which is expected 
  108.      * to be in a format appropriate for this kind of content
  109.      * handler.
  110.      * 
  111.      * @param in  The stream to read from
  112.      * @param doc The destination for the insertion.
  113.      * @param pos The location in the document to place the
  114.      *   content >= 0.
  115.      * @exception IOException on any I/O error
  116.      * @exception BadLocationException if pos represents an invalid
  117.      *   location within the document.
  118.      */
  119.     public void read(InputStream in, Document doc, int pos) 
  120.         throws IOException, BadLocationException {
  121.  
  122.         read(new InputStreamReader(in), doc, pos);
  123.     }
  124.  
  125.     /**
  126.      * Writes content from a document to the given stream
  127.      * in a format appropriate for this kind of content handler.
  128.      * 
  129.      * @param out The stream to write to
  130.      * @param doc The source for the write.
  131.      * @param pos The location in the document to fetch the
  132.      *   content >= 0.
  133.      * @param len The amount to write out >= 0.
  134.      * @exception IOException on any I/O error
  135.      * @exception BadLocationException if pos represents an invalid
  136.      *   location within the document.
  137.      */
  138.     public void write(OutputStream out, Document doc, int pos, int len) 
  139.         throws IOException, BadLocationException {
  140.  
  141.         write(new OutputStreamWriter(out), doc, pos, len);
  142.     }
  143.  
  144.     /**
  145.      * Inserts content from the given stream, which will be 
  146.      * treated as plain text.
  147.      * 
  148.      * @param in  The stream to read from
  149.      * @param doc The destination for the insertion.
  150.      * @param pos The location in the document to place the
  151.      *   content >= 0.
  152.      * @exception IOException on any I/O error
  153.      * @exception BadLocationException if pos represents an invalid
  154.      *   location within the document.
  155.      */
  156.     public void read(Reader in, Document doc, int pos) 
  157.         throws IOException, BadLocationException {
  158.  
  159.         char[] buff = new char[4096];
  160.         int nch;
  161.         while ((nch = in.read(buff, 0, buff.length)) != -1) {
  162.             doc.insertString(pos, new String(buff, 0, nch), null);
  163.         pos += nch;
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Writes content from a document to the given stream
  169.      * as plain text.
  170.      * 
  171.      * @param out  The stream to write to
  172.      * @param doc The source for the write.
  173.      * @param pos The location in the document to fetch the
  174.      *   content from >= 0.
  175.      * @param len The amount to write out >= 0.
  176.      * @exception IOException on any I/O error
  177.      * @exception BadLocationException if pos is not within 0 and
  178.      *   the length of the document.
  179.      */
  180.     public void write(Writer out, Document doc, int pos, int len) 
  181.         throws IOException, BadLocationException {
  182.  
  183.     if ((pos < 0) || ((pos + len) > doc.getLength())) {
  184.         throw new BadLocationException("DefaultEditorKit.write", pos);
  185.     }
  186.         Segment data = new Segment();
  187.         int nleft = len;
  188.         int offs = pos;
  189.         while (nleft > 0) {
  190.             int n = Math.min(nleft, 4096);
  191.             doc.getText(offs, n, data);
  192.             out.write(data.array, data.offset, data.count);
  193.             offs += n;
  194.             nleft -= n;
  195.         }
  196.     out.flush();
  197.     }
  198.  
  199.  
  200.     // --- names of well-known actions ---------------------------
  201.  
  202.     /**
  203.      * Name of the action to place content into the associated
  204.      * document.  If there is a selection, it is removed before
  205.      * the new content is added.
  206.      * @see InsertContentAction
  207.      * @see #getActions
  208.      */
  209.     public static final String insertContentAction = "insert-content";
  210.  
  211.     /**
  212.      * Name of the action to place a line/paragraph break into
  213.      * the document.  If there is a selection, it is removed before
  214.      * the break is added.
  215.      * @see InsertBreakAction
  216.      * @see #getActions
  217.      */
  218.     public static final String insertBreakAction = "insert-break";
  219.  
  220.     /**
  221.      * Name of the action to place a tab character into
  222.      * the document.  If there is a selection, it is removed before
  223.      * the tab is added.
  224.      * @see InsertTabAction
  225.      * @see #getActions
  226.      */
  227.     public static final String insertTabAction = "insert-tab";
  228.  
  229.     /**
  230.      * Name of the action to delete the character of content that
  231.      * precedes the current caret position.
  232.      * @see DeletePrevCharAction
  233.      * @see #getActions
  234.      */
  235.     public static final String deletePrevCharAction = "delete-previous";
  236.  
  237.     /**
  238.      * Name of the action to delete the character of content that
  239.      * follows the current caret position.
  240.      * @see DeleteNextCharAction
  241.      * @see #getActions
  242.      */
  243.     public static final String deleteNextCharAction = "delete-next";
  244.  
  245.     /**
  246.      * Name of the action to set the editor into read-only
  247.      * mode.
  248.      * @see ReadOnlyAction
  249.      * @see #getActions
  250.      */
  251.     public static final String readOnlyAction = "set-read-only";
  252.  
  253.     /**
  254.      * Name of the action to set the editor into writeable
  255.      * mode.
  256.      * @see WritableAction
  257.      * @see #getActions
  258.      */
  259.     public static final String writableAction = "set-writable";
  260.  
  261.     /**
  262.      * Name of the action to cut the selected region
  263.      * and place the contents into the system clipboard.
  264.      * @see JTextComponent#cut
  265.      * @see #getActions
  266.      */
  267.     public static final String cutAction = "cut-to-clipboard";
  268.  
  269.     /**
  270.      * Name of the action to copy the selected region
  271.      * and place the contents into the system clipboard.
  272.      * @see JTextComponent#copy
  273.      * @see #getActions
  274.      */
  275.     public static final String copyAction = "copy-to-clipboard";
  276.  
  277.     /**
  278.      * Name of the action to paste the contents of the
  279.      * system clipboard into the selected region, or before the
  280.      * caret if nothing is selected.
  281.      * @see JTextComponent#paste
  282.      * @see #getActions
  283.      */
  284.     public static final String pasteAction = "paste-from-clipboard";
  285.  
  286.     /**
  287.      * Name of the action to create a beep.
  288.      * @see BeepAction
  289.      * @see #getActions
  290.      */
  291.     public static final String beepAction = "beep";
  292.  
  293.     /**
  294.      * Name of the action to page up vertically.
  295.      * @see PageUpAction
  296.      * @see #getActions
  297.      */
  298.     public static final String pageUpAction = "page-up";
  299.  
  300.     /**
  301.      * Name of the action to page down vertically.
  302.      * @see PageDownAction
  303.      * @see #getActions
  304.      */
  305.     public static final String pageDownAction = "page-down";
  306.  
  307.     /**
  308.      * Name of the Action for moving the caret 
  309.      * logically forward one position.
  310.      * @see ForwardAction
  311.      * @see #getActions
  312.      */
  313.     public static final String forwardAction = "caret-forward";
  314.  
  315.     /**
  316.      * Name of the Action for moving the caret 
  317.      * logically backward one position.
  318.      * @see BackwardAction
  319.      * @see #getActions
  320.      */
  321.     public static final String backwardAction = "caret-backward";
  322.  
  323.     /**
  324.      * Name of the Action for extending the selection
  325.      * by moving the caret logically forward one position.
  326.      * @see SelectionForwardAction
  327.      * @see #getActions
  328.      */
  329.     public static final String selectionForwardAction = "selection-forward";
  330.  
  331.     /**
  332.      * Name of the Action for extending the selection
  333.      * by moving the caret logically backward one position.
  334.      * @see SelectionBackwardAction
  335.      * @see #getActions
  336.      */
  337.     public static final String selectionBackwardAction = "selection-backward";
  338.  
  339.     /**
  340.      * Name of the Action for moving the caret 
  341.      * logically upward one position.
  342.      * @see UpAction
  343.      * @see #getActions
  344.      */
  345.     public static final String upAction = "caret-up";
  346.  
  347.     /**
  348.      * Name of the Action for moving the caret 
  349.      * logically downward one position.
  350.      * @see DownAction
  351.      * @see #getActions
  352.      */
  353.     public static final String downAction = "caret-down";
  354.  
  355.     /**
  356.      * Name of the Action for moving the caret 
  357.      * logically upward one position, extending the selection.
  358.      * @see UpAction
  359.      * @see #getActions
  360.      */
  361.     public static final String selectionUpAction = "selection-up";
  362.  
  363.     /**
  364.      * Name of the Action for moving the caret 
  365.      * logically downward one position, extending the selection.
  366.      * @see DownAction
  367.      * @see #getActions
  368.      */
  369.     public static final String selectionDownAction = "selection-down";
  370.  
  371.     /**
  372.      * Name of the Action for moving the caret 
  373.      * to the begining of a word.
  374.      * @see BeginAction
  375.      * @see #getActions
  376.      */
  377.     public static final String beginWordAction = "caret-begin-word";
  378.  
  379.     /**
  380.      * Name of the Action for moving the caret 
  381.      * to the end of a word.
  382.      * @see EndAction
  383.      * @see #getActions
  384.      */
  385.     public static final String endWordAction = "caret-end-word";
  386.  
  387.     /**
  388.      * Name of the Action for moving the caret 
  389.      * to the begining of a word, extending the selection.
  390.      * @see BeginWordAction
  391.      * @see #getActions
  392.      */
  393.     public static final String selectionBeginWordAction = "selection-begin-word";
  394.  
  395.     /**
  396.      * Name of the Action for moving the caret 
  397.      * to the end of a word, extending the selection.
  398.      * @see EndWordAction
  399.      * @see #getActions
  400.      */
  401.     public static final String selectionEndWordAction = "selection-end-word";
  402.  
  403.     /**
  404.      * Name of the Action for moving the caret to the begining of the
  405.      * previous word.
  406.      * @see PreviousWordAction
  407.      * @see #getActions
  408.      */
  409.     public static final String previousWordAction = "caret-previous-word";
  410.  
  411.     /**
  412.      * Name of the Action for moving the caret to the begining of the
  413.      * next word.
  414.      * to the next of the document.
  415.      * @see NextWordAction
  416.      * @see #getActions
  417.      */
  418.     public static final String nextWordAction = "caret-next-word";
  419.  
  420.     /**
  421.      * Name of the Action for moving the selection to the begining of the
  422.      * previous word, extending the selection.
  423.      * @see PreviousWordAction
  424.      * @see #getActions
  425.      */
  426.     public static final String selectionPreviousWordAction = "selection-previous-word";
  427.  
  428.     /**
  429.      * Name of the Action for moving the selection to the begining of the
  430.      * next word, extending the selection.
  431.      * @see NextWordAction
  432.      * @see #getActions
  433.      */
  434.     public static final String selectionNextWordAction = "selection-next-word";
  435.  
  436.     /**
  437.      * Name of the Action for moving the caret 
  438.      * to the begining of a line.
  439.      * @see BeginAction
  440.      * @see #getActions
  441.      */
  442.     public static final String beginLineAction = "caret-begin-line";
  443.  
  444.     /**
  445.      * Name of the Action for moving the caret 
  446.      * to the end of a line.
  447.      * @see EndAction
  448.      * @see #getActions
  449.      */
  450.     public static final String endLineAction = "caret-end-line";
  451.  
  452.     /**
  453.      * Name of the Action for moving the caret 
  454.      * to the begining of a line, extending the selection.
  455.      * @see BeginLineAction
  456.      * @see #getActions
  457.      */
  458.     public static final String selectionBeginLineAction = "selection-begin-line";
  459.  
  460.     /**
  461.      * Name of the Action for moving the caret 
  462.      * to the end of a line, extending the selection.
  463.      * @see EndLineAction
  464.      * @see #getActions
  465.      */
  466.     public static final String selectionEndLineAction = "selection-end-line";
  467.  
  468.     /**
  469.      * Name of the Action for moving the caret 
  470.      * to the begining of a paragraph.
  471.      * @see BeginAction
  472.      * @see #getActions
  473.      */
  474.     public static final String beginParagraphAction = "caret-begin-paragraph";
  475.  
  476.     /**
  477.      * Name of the Action for moving the caret 
  478.      * to the end of a paragraph.
  479.      * @see EndAction
  480.      * @see #getActions
  481.      */
  482.     public static final String endParagraphAction = "caret-end-paragraph";
  483.  
  484.     /**
  485.      * Name of the Action for moving the caret 
  486.      * to the begining of a paragraph, extending the selection.
  487.      * @see BeginParagraphAction
  488.      * @see #getActions
  489.      */
  490.     public static final String selectionBeginParagraphAction = "selection-begin-paragraph";
  491.  
  492.     /**
  493.      * Name of the Action for moving the caret 
  494.      * to the end of a paragraph, extending the selection.
  495.      * @see EndParagraphAction
  496.      * @see #getActions
  497.      */
  498.     public static final String selectionEndParagraphAction = "selection-end-paragraph";
  499.  
  500.     /**
  501.      * Name of the Action for moving the caret 
  502.      * to the begining of the document.
  503.      * @see BeginAction
  504.      * @see #getActions
  505.      */
  506.     public static final String beginAction = "caret-begin";
  507.  
  508.     /**
  509.      * Name of the Action for moving the caret 
  510.      * to the end of the document.
  511.      * @see EndAction
  512.      * @see #getActions
  513.      */
  514.     public static final String endAction = "caret-end";
  515.  
  516.     /**
  517.      * Name of the Action for moving the caret 
  518.      * to the begining of the document.
  519.      * @see BeginAction
  520.      * @see #getActions
  521.      */
  522.     public static final String selectionBeginAction = "selection-begin";
  523.  
  524.     /**
  525.      * Name of the Action for moving the caret 
  526.      * to the end of the document.
  527.      * @see EndAction
  528.      * @see #getActions
  529.      */
  530.     public static final String selectionEndAction = "selection-end";
  531.  
  532.     /**
  533.      * Name of the Action for selecting a word around the caret.
  534.      * @see SelectWordAction
  535.      * @see #getActions
  536.      */
  537.     public static final String selectWordAction = "select-word";
  538.  
  539.     /**
  540.      * Name of the Action for selecting a line around the caret.
  541.      * @see SelectLineAction
  542.      * @see #getActions
  543.      */
  544.     public static final String selectLineAction = "select-line";
  545.  
  546.     /**
  547.      * Name of the Action for selecting a paragraph around the caret.
  548.      * @see SelectParagraphAction
  549.      * @see #getActions
  550.      */
  551.     public static final String selectParagraphAction = "select-paragraph";
  552.  
  553.     /**
  554.      * Name of the Action for selecting the entire document
  555.      * @see SelectAllAction
  556.      * @see #getActions
  557.      */
  558.     public static final String selectAllAction = "select-all";
  559.  
  560.     /**
  561.      * Name of the action that is executed by default if 
  562.      * a <em>key typed event</em> is received and there
  563.      * is no keymap entry.
  564.      * @see DefaultKeyTypedAction
  565.      * @see #getActions
  566.      */
  567.     public static final String defaultKeyTypedAction = "default-typed";
  568.  
  569.     // --- Action implementations ---------------------------------
  570.  
  571.     private static final Action[] defaultActions = {
  572.         new InsertContentAction(), new DeletePrevCharAction(), 
  573.         new DeleteNextCharAction(), new ReadOnlyAction(),
  574.         new WritableAction(), new CutAction(), 
  575.         new CopyAction(), new PasteAction(),
  576.         new PageUpAction(), new PageDownAction(),
  577.         new InsertBreakAction(), new BeepAction(),
  578.         new ForwardAction(forwardAction, false),
  579.         new BackwardAction(backwardAction, false),
  580.         new ForwardAction(selectionForwardAction, true),
  581.         new BackwardAction(selectionBackwardAction, true),
  582.         new UpAction(upAction, false),  
  583.         new DownAction(downAction, false), 
  584.         new UpAction(selectionUpAction, true),  
  585.         new DownAction(selectionDownAction, true), 
  586.         new BeginWordAction(beginWordAction, false),  
  587.         new EndWordAction(endWordAction, false),
  588.         new BeginWordAction(selectionBeginWordAction, true),  
  589.         new EndWordAction(selectionEndWordAction, true),
  590.         new PreviousWordAction(previousWordAction, false),  
  591.         new NextWordAction(nextWordAction, false),
  592.         new PreviousWordAction(selectionPreviousWordAction, true),  
  593.         new NextWordAction(selectionNextWordAction, true),
  594.         new BeginLineAction(beginLineAction, false),  
  595.         new EndLineAction(endLineAction, false),
  596.         new BeginLineAction(selectionBeginLineAction, true),  
  597.         new EndLineAction(selectionEndLineAction, true),
  598.         new BeginParagraphAction(beginParagraphAction, false),  
  599.         new EndParagraphAction(endParagraphAction, false),
  600.         new BeginParagraphAction(selectionBeginParagraphAction, true),  
  601.         new EndParagraphAction(selectionEndParagraphAction, true),
  602.         new BeginAction(beginAction, false), 
  603.         new EndAction(endAction, false),
  604.         new BeginAction(selectionBeginAction, true), 
  605.         new EndAction(selectionEndAction, true),
  606.         new DefaultKeyTypedAction(), new InsertTabAction(),
  607.         new SelectWordAction(), new SelectLineAction(),
  608.         new SelectParagraphAction(), new SelectAllAction(),
  609.         new DumpModelAction()
  610.     };
  611.  
  612.     /**
  613.      * The action that is executed by default if 
  614.      * a <em>key typed event</em> is received and there
  615.      * is no keymap entry.  There is a variation across
  616.      * different VM's in what gets sent as a <em>key typed</em>
  617.      * event, and this action tries to filter out the undesired
  618.      * events.  This filters the control characters and those
  619.      * with the ALT modifier.
  620.      * <p>
  621.      * If the event doesn't get filtered, it will try to insert
  622.      * content into the text editor.  The content is fetched
  623.      * from the command string of the ActionEvent.  The text
  624.      * entry is done through the <code>replaceSelection</code>
  625.      * method on the target text component.  This is the
  626.      * action that will be fired for most text entry tasks.
  627.      * <p>
  628.      * Warning: serialized objects of this class will not be compatible with
  629.      * future swing releases.  The current serialization support is appropriate
  630.      * for short term storage or RMI between Swing1.0 applications.  It will
  631.      * not be possible to load serialized Swing1.0 objects with future releases
  632.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  633.      * baseline for the serialized form of Swing objects.
  634.      *
  635.      * @see DefaultEditorKit#defaultKeyTypedAction
  636.      * @see DefaultEditorKit#getActions
  637.      * @see Keymap#setDefaultAction
  638.      * @see Keymap#getDefaultAction
  639.      */
  640.     public static class DefaultKeyTypedAction extends TextAction {
  641.  
  642.         /**
  643.          * Creates this object with the appropriate identifier.
  644.          */
  645.         public DefaultKeyTypedAction() {
  646.             super(defaultKeyTypedAction);
  647.         }
  648.  
  649.         /**
  650.          * The operation to perform when this action is triggered.
  651.          *
  652.          * @param e the action event
  653.          */
  654.         public void actionPerformed(ActionEvent e) {
  655.             JTextComponent target = getTextComponent(e);
  656.             if ((target != null) && (e != null)) {
  657.                 String content = e.getActionCommand();
  658.                 int mod = e.getModifiers();
  659.                 if ((content != null) && (content.length() > 0) && 
  660.                     ((mod & ActionEvent.ALT_MASK) == 0)) {
  661.                     char c = content.charAt(0);
  662.                     if ((c >= 0x20) && (c != 0x7F)) {
  663.                         target.replaceSelection(content);
  664.                     }
  665.                 }
  666.             }
  667.         }
  668.     }
  669.  
  670.     /**
  671.      * Places content into the associated document.
  672.      * If there is a selection, it is removed before
  673.      * the new content is added.
  674.      * <p>
  675.      * Warning: serialized objects of this class will not be compatible with
  676.      * future swing releases.  The current serialization support is appropriate
  677.      * for short term storage or RMI between Swing1.0 applications.  It will
  678.      * not be possible to load serialized Swing1.0 objects with future releases
  679.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  680.      * baseline for the serialized form of Swing objects.
  681.      *
  682.      * @see DefaultEditorKit#insertContentAction
  683.      * @see DefaultEditorKit#getActions
  684.      */
  685.     public static class InsertContentAction extends TextAction {
  686.  
  687.         /**
  688.          * Creates this object with the appropriate identifier.
  689.          */
  690.         public InsertContentAction() {
  691.             super(insertContentAction);
  692.         }
  693.  
  694.         /**
  695.          * The operation to perform when this action is triggered.
  696.          *
  697.          * @param e the action event
  698.          */
  699.         public void actionPerformed(ActionEvent e) {
  700.             JTextComponent target = getTextComponent(e);
  701.             if ((target != null) && (e != null)) {
  702.                 String content = e.getActionCommand();
  703.                 if (content != null) {
  704.                     target.replaceSelection(content);
  705.                 } else {
  706.                     target.getToolkit().beep();
  707.                 }
  708.             }
  709.         }
  710.     }
  711.  
  712.     /**
  713.      * Places a line/paragraph break into the document.
  714.      * If there is a selection, it is removed before
  715.      * the break is added.
  716.      * <p>
  717.      * Warning: serialized objects of this class will not be compatible with
  718.      * future swing releases.  The current serialization support is appropriate
  719.      * for short term storage or RMI between Swing1.0 applications.  It will
  720.      * not be possible to load serialized Swing1.0 objects with future releases
  721.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  722.      * baseline for the serialized form of Swing objects.
  723.      *
  724.      * @see DefaultEditorKit#insertBreakAction
  725.      * @see DefaultEditorKit#getActions
  726.      */
  727.     public static class InsertBreakAction extends TextAction {
  728.  
  729.         /**
  730.          * Creates this object with the appropriate identifier.
  731.          */
  732.         public InsertBreakAction() {
  733.             super(insertBreakAction);
  734.         }
  735.  
  736.         /**
  737.          * The operation to perform when this action is triggered.
  738.          *
  739.          * @param e the action event
  740.          */
  741.         public void actionPerformed(ActionEvent e) {
  742.             JTextComponent target = getTextComponent(e);
  743.             if (target != null) {
  744.                 target.replaceSelection("\n");
  745.             }
  746.         }
  747.     }
  748.  
  749.     /**
  750.      * Places a tab character into the document. If there
  751.      * is a selection, it is removed before the tab is added.
  752.      * <p>
  753.      * Warning: serialized objects of this class will not be compatible with
  754.      * future swing releases.  The current serialization support is appropriate
  755.      * for short term storage or RMI between Swing1.0 applications.  It will
  756.      * not be possible to load serialized Swing1.0 objects with future releases
  757.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  758.      * baseline for the serialized form of Swing objects.
  759.      *
  760.      * @see DefaultEditorKit#insertTabAction
  761.      * @see DefaultEditorKit#getActions
  762.      */
  763.     public static class InsertTabAction extends TextAction {
  764.  
  765.         /**
  766.          * Creates this object with the appropriate identifier.
  767.          */
  768.         public InsertTabAction() {
  769.             super(insertTabAction);
  770.         }
  771.  
  772.         /**
  773.          * The operation to perform when this action is triggered.
  774.          *
  775.          * @param e the action event
  776.          */
  777.         public void actionPerformed(ActionEvent e) {
  778.             JTextComponent target = getTextComponent(e);
  779.             if (target != null) {
  780.                 target.replaceSelection("\t");
  781.             }
  782.         }
  783.     }
  784.  
  785.     /*
  786.      * Deletes the character of content that precedes the
  787.      * current caret position.
  788.      * @see DefaultEditorKit#deletePrevCharAction
  789.      * @see DefaultEditorKit#getActions
  790.      */
  791.     static class DeletePrevCharAction extends TextAction {
  792.  
  793.         /**
  794.          * Creates this object with the appropriate identifier.
  795.          */
  796.         DeletePrevCharAction() {
  797.             super(deletePrevCharAction);
  798.         }
  799.  
  800.         /**
  801.          * The operation to perform when this action is triggered.
  802.          *
  803.          * @param e the action event
  804.          */
  805.         public void actionPerformed(ActionEvent e) {
  806.             JTextComponent target = getTextComponent(e);
  807.             boolean beep = true;
  808.             if ((target != null) && (target.isEditable())) {
  809.                 try {
  810.                     Document doc = target.getDocument();
  811.                     Caret caret = target.getCaret();
  812.                     int dot = caret.getDot();
  813.                     int mark = caret.getMark();
  814.                     if (dot != mark) {
  815.                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
  816.                         beep = false;
  817.                     } else if (dot > 0) {
  818.                         doc.remove(dot - 1, 1);
  819.                         beep = false;
  820.                     }
  821.                 } catch (BadLocationException bl) {
  822.                 }
  823.             }
  824.             if (beep) {
  825.                 Toolkit.getDefaultToolkit().beep();
  826.             }
  827.         }
  828.     }
  829.  
  830.     /*
  831.      * Deletes the character of content that follows the
  832.      * current caret position.
  833.      * @see DefaultEditorKit#deleteNextCharAction
  834.      * @see DefaultEditorKit#getActions
  835.      */
  836.     static class DeleteNextCharAction extends TextAction {
  837.  
  838.         /* Create this object with the appropriate identifier. */
  839.         DeleteNextCharAction() {
  840.             super(deleteNextCharAction);
  841.         }
  842.  
  843.         /** The operation to perform when this action is triggered. */
  844.         public void actionPerformed(ActionEvent e) {
  845.             JTextComponent target = getTextComponent(e);
  846.             boolean beep = true;
  847.             if ((target != null) && (target.isEditable())) {
  848.                 try {
  849.                     Document doc = target.getDocument();
  850.                     Caret caret = target.getCaret();
  851.                     int dot = caret.getDot();
  852.                     int mark = caret.getMark();
  853.                     if (dot != mark) {
  854.                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
  855.                         beep = false;
  856.                     } else if (dot < doc.getLength()) {
  857.                         doc.remove(dot, 1);
  858.                         beep = false;
  859.                     }
  860.                 } catch (BadLocationException bl) {
  861.                 }
  862.             }
  863.             if (beep) {
  864.                 Toolkit.getDefaultToolkit().beep();
  865.             }
  866.         }
  867.     }
  868.  
  869.     /*
  870.      * Sets the editor into read-only mode.
  871.      * @see DefaultEditorKit#readOnlyAction
  872.      * @see DefaultEditorKit#getActions
  873.      */
  874.     static class ReadOnlyAction extends TextAction {
  875.  
  876.         /* Create this object with the appropriate identifier. */
  877.         ReadOnlyAction() {
  878.             super(readOnlyAction);
  879.         }
  880.  
  881.         /**
  882.          * The operation to perform when this action is triggered.
  883.          *
  884.          * @param e the action event
  885.          */
  886.         public void actionPerformed(ActionEvent e) {
  887.             JTextComponent target = getTextComponent(e);
  888.             if (target != null) {
  889.                 target.setEditable(false);
  890.             }
  891.         }
  892.     }
  893.  
  894.     /*
  895.      * Sets the editor into writeable mode.
  896.      * @see DefaultEditorKit#writableAction
  897.      * @see DefaultEditorKit#getActions
  898.      */
  899.     static class WritableAction extends TextAction {
  900.  
  901.         /* Create this object with the appropriate identifier. */
  902.         WritableAction() {
  903.             super(writableAction);
  904.         }
  905.  
  906.         /**
  907.          * The operation to perform when this action is triggered.
  908.          *
  909.          * @param e the action event
  910.          */
  911.         public void actionPerformed(ActionEvent e) {
  912.             JTextComponent target = getTextComponent(e);
  913.             if (target != null) {
  914.                 target.setEditable(true);
  915.             }
  916.         }
  917.     }
  918.  
  919.     /**
  920.      * Cuts the selected region and place its contents
  921.      * into the system clipboard.
  922.      * <p>
  923.      * Warning: serialized objects of this class will not be compatible with
  924.      * future swing releases.  The current serialization support is appropriate
  925.      * for short term storage or RMI between Swing1.0 applications.  It will
  926.      * not be possible to load serialized Swing1.0 objects with future releases
  927.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  928.      * baseline for the serialized form of Swing objects.
  929.      *
  930.      * @see DefaultEditorKit#cutAction
  931.      * @see DefaultEditorKit#getActions
  932.      */
  933.     public static class CutAction extends TextAction {
  934.  
  935.         /** Create this object with the appropriate identifier. */
  936.         public CutAction() {
  937.             super(cutAction);
  938.         }
  939.  
  940.         /**
  941.          * The operation to perform when this action is triggered.
  942.          *
  943.          * @param e the action event
  944.          */
  945.         public void actionPerformed(ActionEvent e) {
  946.             JTextComponent target = getTextComponent(e);
  947.             if (target != null) {
  948.                 target.cut();
  949.             }
  950.         }
  951.     }
  952.  
  953.     /**
  954.      * Coies the selected region and place its contents
  955.      * into the system clipboard.
  956.      * <p>
  957.      * Warning: serialized objects of this class will not be compatible with
  958.      * future swing releases.  The current serialization support is appropriate
  959.      * for short term storage or RMI between Swing1.0 applications.  It will
  960.      * not be possible to load serialized Swing1.0 objects with future releases
  961.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  962.      * baseline for the serialized form of Swing objects.
  963.      *
  964.      * @see DefaultEditorKit#copyAction
  965.      * @see DefaultEditorKit#getActions
  966.      */
  967.     public static class CopyAction extends TextAction {
  968.  
  969.         /** Create this object with the appropriate identifier. */
  970.         public CopyAction() {
  971.             super(copyAction);
  972.         }
  973.  
  974.         /**
  975.          * The operation to perform when this action is triggered.
  976.          *
  977.          * @param e the action event
  978.          */
  979.         public void actionPerformed(ActionEvent e) {
  980.             JTextComponent target = getTextComponent(e);
  981.             if (target != null) {
  982.                 target.copy();
  983.             }
  984.         }
  985.     }
  986.  
  987.     /**
  988.      * Pastes the contents of the system clipboard into the
  989.      * selected region, or before the caret if nothing is
  990.      * selected.
  991.      * <p>
  992.      * Warning: serialized objects of this class will not be compatible with
  993.      * future swing releases.  The current serialization support is appropriate
  994.      * for short term storage or RMI between Swing1.0 applications.  It will
  995.      * not be possible to load serialized Swing1.0 objects with future releases
  996.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  997.      * baseline for the serialized form of Swing objects.
  998.      *
  999.      * @see DefaultEditorKit#pasteAction
  1000.      * @see DefaultEditorKit#getActions
  1001.      */
  1002.     public static class PasteAction extends TextAction {
  1003.  
  1004.         /** Create this object with the appropriate identifier. */
  1005.         public PasteAction() {
  1006.             super(pasteAction);
  1007.         }
  1008.  
  1009.         /**
  1010.          * The operation to perform when this action is triggered.
  1011.          *
  1012.          * @param e the action event
  1013.          */
  1014.         public void actionPerformed(ActionEvent e) {
  1015.             JTextComponent target = getTextComponent(e);
  1016.             if (target != null) {
  1017.                 target.paste();
  1018.             }
  1019.         }
  1020.     }
  1021.  
  1022.     /**
  1023.      * Creates a beep.
  1024.      * <p>
  1025.      * Warning: serialized objects of this class will not be compatible with
  1026.      * future swing releases.  The current serialization support is appropriate
  1027.      * for short term storage or RMI between Swing1.0 applications.  It will
  1028.      * not be possible to load serialized Swing1.0 objects with future releases
  1029.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  1030.      * baseline for the serialized form of Swing objects.
  1031.      *
  1032.      * @see DefaultEditorKit#beepAction
  1033.      * @see DefaultEditorKit#getActions
  1034.      */
  1035.     public static class BeepAction extends TextAction {
  1036.  
  1037.         /** Create this object with the appropriate identifier. */
  1038.         public BeepAction() {
  1039.             super(beepAction);
  1040.         }
  1041.  
  1042.         /**
  1043.          * The operation to perform when this action is triggered.
  1044.          *
  1045.          * @param e the action event
  1046.          */
  1047.         public void actionPerformed(ActionEvent e) {
  1048.             Toolkit.getDefaultToolkit().beep();
  1049.         }
  1050.     }
  1051.  
  1052.     /*
  1053.      * Pages up vertically.
  1054.      * @see DefaultEditorKit#pageUpAction
  1055.      * @see DefaultEditorKit#getActions
  1056.      */
  1057.     static class PageUpAction extends TextAction {
  1058.  
  1059.         /* Create this object with the appropriate identifier. */
  1060.         PageUpAction() {
  1061.             super(pageUpAction);
  1062.         }
  1063.  
  1064.         /** The operation to perform when this action is triggered. */
  1065.         public void actionPerformed(ActionEvent e) {
  1066.             JTextComponent target = getTextComponent(e);
  1067.             if (target != null) {
  1068.         int scrollOffset;
  1069.         int selectedIndex;
  1070.         Rectangle visible = new Rectangle();
  1071.         Rectangle r;
  1072.         target.computeVisibleRect(visible);
  1073.         scrollOffset = visible.y;
  1074.         visible.y -= visible.height;
  1075.         if(visible.y < 0)
  1076.             visible.y = 0;
  1077.         scrollOffset = scrollOffset - visible.y;
  1078.         target.scrollRectToVisible(visible);
  1079.         
  1080.         selectedIndex = target.getCaretPosition();
  1081.         try {
  1082.             if(selectedIndex != -1) {
  1083.             r = target.modelToView(selectedIndex);
  1084.             r.y -= scrollOffset;
  1085.             selectedIndex = target.viewToModel(new Point(r.x,r.y));
  1086.             if(selectedIndex  < 0) {
  1087.                 selectedIndex = 0;
  1088.             }
  1089.             target.setCaretPosition(selectedIndex);
  1090.             }
  1091.         } catch(BadLocationException bl) {
  1092.             target.getToolkit().beep();
  1093.         }
  1094.             }
  1095.         }
  1096.     }
  1097.  
  1098.     /*
  1099.      * Pages down vertically.
  1100.      * @see DefaultEditorKit#pageDownAction
  1101.      * @see DefaultEditorKit#getActions
  1102.      */
  1103.     static class PageDownAction extends TextAction {
  1104.  
  1105.         /* Create this object with the appropriate identifier. */
  1106.         PageDownAction() {
  1107.             super(pageDownAction);
  1108.         }
  1109.  
  1110.         /** The operation to perform when this action is triggered. */
  1111.         public void actionPerformed(ActionEvent e) {
  1112.             JTextComponent target = getTextComponent(e);
  1113.             if (target != null) {
  1114.         int scrollOffset;
  1115.         int selectedIndex;
  1116.         Rectangle visible = new Rectangle();
  1117.         Rectangle r;
  1118.         target.computeVisibleRect(visible);
  1119.         scrollOffset = visible.y;
  1120.         visible.y += visible.height;
  1121.         if((visible.y+visible.height) > target.getHeight())
  1122.             visible.y = (target.getHeight() - visible.height);
  1123.         scrollOffset = visible.y - scrollOffset;
  1124.         target.scrollRectToVisible(visible);
  1125.         
  1126.         selectedIndex = target.getCaretPosition();
  1127.         try {
  1128.             if(selectedIndex != -1) {
  1129.             r = target.modelToView(selectedIndex);
  1130.             r.y += scrollOffset;
  1131.             selectedIndex = target.viewToModel(new Point(r.x,r.y));
  1132.             Document doc = target.getDocument();
  1133.             if(selectedIndex  > (doc.getLength()-1))
  1134.                 selectedIndex = doc.getLength()-1;
  1135.             target.setCaretPosition(selectedIndex);
  1136.             }
  1137.         } catch(BadLocationException bl) {
  1138.             target.getToolkit().beep();
  1139.         }
  1140.             }
  1141.         }
  1142.     }
  1143.  
  1144.     static class DumpModelAction extends TextAction {
  1145.  
  1146.         DumpModelAction() {
  1147.             super("dump-model");
  1148.         }
  1149.  
  1150.         public void actionPerformed(ActionEvent e) {
  1151.             JTextComponent target = getTextComponent(e);
  1152.             if (target != null) {
  1153.                 Document d = target.getDocument();
  1154.                 if (d instanceof AbstractDocument) {
  1155.                     ((AbstractDocument) d).dump(System.err);
  1156.                 }
  1157.             }
  1158.         }
  1159.     }
  1160.  
  1161.     /*
  1162.      * Move the caret logically forward one position.
  1163.      * @see DefaultEditorKit#forwardAction
  1164.      * @see DefaultEditorKit#getActions
  1165.      */
  1166.     static class ForwardAction extends TextAction {
  1167.  
  1168.         /** 
  1169.          * Create this action with the appropriate identifier. 
  1170.          * @param nm  the name of the action, Action.NAME.
  1171.          * @param select whether to extend the selection when
  1172.          *  changing the caret position.
  1173.          */
  1174.         ForwardAction(String nm, boolean select) {
  1175.             super(nm);
  1176.             this.select = select;
  1177.         }
  1178.  
  1179.         /** The operation to perform when this action is triggered. */
  1180.         public void actionPerformed(ActionEvent e) {
  1181.             JTextComponent target = getTextComponent(e);
  1182.             if (target != null) {
  1183.                 Document doc = target.getDocument();
  1184.                 int dot = target.getCaretPosition();
  1185.                 if (dot < doc.getLength()) {
  1186.                     dot += 1;
  1187.                     if (select) {
  1188.                         target.moveCaretPosition(dot);
  1189.                     } else {
  1190.                         target.setCaretPosition(dot);
  1191.                     }
  1192.                 } else {
  1193.                     target.getToolkit().beep();
  1194.                 }
  1195.                 target.getCaret().setMagicCaretPosition(null);
  1196.             }
  1197.         }
  1198.  
  1199.         private boolean select;
  1200.     }
  1201.             
  1202.     /*
  1203.      * Move the caret logically backward one position.
  1204.      * @see DefaultEditorKit#backwardAction
  1205.      * @see DefaultEditorKit#getActions
  1206.      */
  1207.     static class BackwardAction extends TextAction {
  1208.  
  1209.         /** 
  1210.          * Create this action with the appropriate identifier. 
  1211.          * @param nm  the name of the action, Action.NAME.
  1212.          * @param select whether to extend the selection when
  1213.          *  changing the caret position.
  1214.          */
  1215.         BackwardAction(String nm, boolean select) {
  1216.             super(nm);
  1217.             this.select = select;
  1218.         }
  1219.  
  1220.         /** The operation to perform when this action is triggered. */
  1221.         public void actionPerformed(ActionEvent e) {
  1222.             JTextComponent target = getTextComponent(e);
  1223.             if (target != null) {
  1224.                 int dot = target.getCaretPosition();
  1225.                 if (dot > 0) {
  1226.                     dot -= 1;
  1227.                     if (select) {
  1228.                         target.moveCaretPosition(dot);
  1229.                     } else {
  1230.                         target.setCaretPosition(dot);
  1231.                     }
  1232.                 } else {
  1233.                     target.getToolkit().beep();
  1234.                 }
  1235.                 target.getCaret().setMagicCaretPosition(null);
  1236.             }
  1237.         }
  1238.  
  1239.         private boolean select;
  1240.     }
  1241.             
  1242.     /*
  1243.      * Move the caret upward one row visually.
  1244.      * @see DefaultEditorKit#upAction
  1245.      * @see DefaultEditorKit#getActions
  1246.      */
  1247.     static class UpAction extends TextAction {
  1248.  
  1249.         /** 
  1250.          * Create this action with the appropriate identifier. 
  1251.          * @param nm  the name of the action, Action.NAME.
  1252.          * @param select whether to extend the selection when
  1253.          *  changing the caret position.
  1254.          */
  1255.         UpAction(String nm, boolean select) {
  1256.             super(nm);
  1257.             this.select = select;
  1258.         }
  1259.  
  1260.         /** The operation to perform when this action is triggered. */
  1261.         public void actionPerformed(ActionEvent e) {
  1262.             JTextComponent target = getTextComponent(e);
  1263.             if (target != null) {
  1264.                 try {
  1265.                     Caret caret = target.getCaret();
  1266.                     int dot = caret.getDot();
  1267.                     Point p = caret.getMagicCaretPosition();
  1268.                     if (p == null) {
  1269.                         Rectangle r = target.modelToView(dot);
  1270.                         p = new Point(r.x, r.y);
  1271.                         caret.setMagicCaretPosition(p);
  1272.                     }
  1273.                     dot = Utilities.getPositionAbove(target, dot, p.x);
  1274.                     if (select) {
  1275.                         caret.moveDot(dot);
  1276.                     } else {
  1277.                         caret.setDot(dot);
  1278.                     }
  1279.                 } catch (BadLocationException ex) {
  1280.                     target.getToolkit().beep();
  1281.                 }
  1282.             }
  1283.         }
  1284.  
  1285.         private boolean select;
  1286.     }
  1287.  
  1288.     /*
  1289.      * Move the caret downward one row visually.
  1290.      * @see DefaultEditorKit#downAction
  1291.      * @see DefaultEditorKit#getActions
  1292.      */
  1293.     static class DownAction extends TextAction {
  1294.  
  1295.         /** 
  1296.          * Create this action with the appropriate identifier. 
  1297.          * @param nm  the name of the action, Action.NAME.
  1298.          * @param select whether to extend the selection when
  1299.          *  changing the caret position.
  1300.          */
  1301.         DownAction(String nm, boolean select) {
  1302.             super(nm);
  1303.             this.select = select;
  1304.         }
  1305.  
  1306.         /** The operation to perform when this action is triggered. */
  1307.         public void actionPerformed(ActionEvent e) {
  1308.             JTextComponent target = getTextComponent(e);
  1309.             if (target != null) {
  1310.                 try {
  1311.                     Caret caret = target.getCaret();
  1312.                     int dot = caret.getDot();
  1313.                     Point p = caret.getMagicCaretPosition();
  1314.                     if (p == null) {
  1315.                         Rectangle r = target.modelToView(dot);
  1316.                         p = new Point(r.x, r.y);
  1317.                         caret.setMagicCaretPosition(p);
  1318.                     }
  1319.                     dot = Utilities.getPositionBelow(target, dot, p.x);
  1320.                     if (select) {
  1321.                         caret.moveDot(dot);
  1322.                     } else {
  1323.                         caret.setDot(dot);
  1324.                     }
  1325.                 } catch (BadLocationException ex) {
  1326.                     target.getToolkit().beep();
  1327.                 }
  1328.             }
  1329.         }
  1330.  
  1331.         private boolean select;
  1332.     }
  1333.             
  1334.     /*
  1335.      * Position the caret to the beginning of the word.
  1336.      * @see DefaultEditorKit#beginWordAction
  1337.      * @see DefaultEditorKit#selectBeginWordAction
  1338.      * @see DefaultEditorKit#getActions
  1339.      */
  1340.     static class BeginWordAction extends TextAction {
  1341.  
  1342.         /** 
  1343.          * Create this action with the appropriate identifier. 
  1344.          * @param nm  the name of the action, Action.NAME.
  1345.          * @param select whether to extend the selection when
  1346.          *  changing the caret position.
  1347.          */
  1348.         BeginWordAction(String nm, boolean select) {
  1349.             super(nm);
  1350.             this.select = select;
  1351.         }
  1352.  
  1353.         /** The operation to perform when this action is triggered. */
  1354.         public void actionPerformed(ActionEvent e) {
  1355.             JTextComponent target = getTextComponent(e);
  1356.             if (target != null) {
  1357.                 try {
  1358.                     int offs = target.getCaretPosition();
  1359.                     int begOffs = Utilities.getWordStart(target, offs);
  1360.                     if (select) {
  1361.                         target.moveCaretPosition(begOffs);
  1362.                     } else {
  1363.                         target.setCaretPosition(begOffs);
  1364.                     }
  1365.                 } catch (BadLocationException bl) {
  1366.                     target.getToolkit().beep();
  1367.                 }
  1368.             }
  1369.         }
  1370.  
  1371.         private boolean select;
  1372.     }
  1373.  
  1374.     /*
  1375.      * Position the caret to the end of the word.
  1376.      * @see DefaultEditorKit#endWordAction
  1377.      * @see DefaultEditorKit#selectEndWordAction
  1378.      * @see DefaultEditorKit#getActions
  1379.      */
  1380.     static class EndWordAction extends TextAction {
  1381.  
  1382.         /** 
  1383.          * Create this action with the appropriate identifier. 
  1384.          * @param nm  the name of the action, Action.NAME.
  1385.          * @param select whether to extend the selection when
  1386.          *  changing the caret position.
  1387.          */
  1388.         EndWordAction(String nm, boolean select) {
  1389.             super(nm);
  1390.             this.select = select;
  1391.         }
  1392.  
  1393.         /** The operation to perform when this action is triggered. */
  1394.         public void actionPerformed(ActionEvent e) {
  1395.             JTextComponent target = getTextComponent(e);
  1396.             if (target != null) {
  1397.                 try {
  1398.                     int offs = target.getCaretPosition();
  1399.                     int endOffs = Utilities.getWordEnd(target, offs);
  1400.                     if (select) {
  1401.                         target.moveCaretPosition(endOffs);
  1402.                     } else {
  1403.                         target.setCaretPosition(endOffs);
  1404.                     }
  1405.                 } catch (BadLocationException bl) {
  1406.                     target.getToolkit().beep();
  1407.                 }
  1408.             }
  1409.         }
  1410.  
  1411.         private boolean select;
  1412.     }
  1413.  
  1414.     /*
  1415.      * Position the caret to the previousning of the word.
  1416.      * @see DefaultEditorKit#previousWordAction
  1417.      * @see DefaultEditorKit#selectPreviousWordAction
  1418.      * @see DefaultEditorKit#getActions
  1419.      */
  1420.     static class PreviousWordAction extends TextAction {
  1421.  
  1422.         /** 
  1423.          * Create this action with the appropriate identifier. 
  1424.          * @param nm  the name of the action, Action.NAME.
  1425.          * @param select whether to extend the selection when
  1426.          *  changing the caret position.
  1427.          */
  1428.         PreviousWordAction(String nm, boolean select) {
  1429.             super(nm);
  1430.             this.select = select;
  1431.         }
  1432.  
  1433.         /** The operation to perform when this action is triggered. */
  1434.         public void actionPerformed(ActionEvent e) {
  1435.             JTextComponent target = getTextComponent(e);
  1436.             if (target != null) {
  1437.                 try {
  1438.                     int offs = target.getCaretPosition();
  1439.                     offs = Utilities.getPreviousWord(target, offs);
  1440.                     if (select) {
  1441.                         target.moveCaretPosition(offs);
  1442.                     } else {
  1443.                         target.setCaretPosition(offs);
  1444.                     }
  1445.                 } catch (BadLocationException bl) {
  1446.                     target.getToolkit().beep();
  1447.                 }
  1448.             }
  1449.         }
  1450.  
  1451.         private boolean select;
  1452.     }
  1453.  
  1454.     /*
  1455.      * Position the caret to the next of the word.
  1456.      * @see DefaultEditorKit#nextWordAction
  1457.      * @see DefaultEditorKit#selectNextWordAction
  1458.      * @see DefaultEditorKit#getActions
  1459.      */
  1460.     static class NextWordAction extends TextAction {
  1461.  
  1462.         /** 
  1463.          * Create this action with the appropriate identifier. 
  1464.          * @param nm  the name of the action, Action.NAME.
  1465.          * @param select whether to extend the selection when
  1466.          *  changing the caret position.
  1467.          */
  1468.         NextWordAction(String nm, boolean select) {
  1469.             super(nm);
  1470.             this.select = select;
  1471.         }
  1472.  
  1473.         /** The operation to perform when this action is triggered. */
  1474.         public void actionPerformed(ActionEvent e) {
  1475.             JTextComponent target = getTextComponent(e);
  1476.             if (target != null) {
  1477.                 try {
  1478.                     int offs = target.getCaretPosition();
  1479.                     offs = Utilities.getNextWord(target, offs);
  1480.                     if (select) {
  1481.                         target.moveCaretPosition(offs);
  1482.                     } else {
  1483.                         target.setCaretPosition(offs);
  1484.                     }
  1485.                 } catch (BadLocationException bl) {
  1486.                     target.getToolkit().beep();
  1487.                 }
  1488.             }
  1489.         }
  1490.  
  1491.         private boolean select;
  1492.     }
  1493.  
  1494.     /*
  1495.      * Position the caret to the beginning of the line.
  1496.      * @see DefaultEditorKit#beginLineAction
  1497.      * @see DefaultEditorKit#selectBeginLineAction
  1498.      * @see DefaultEditorKit#getActions
  1499.      */
  1500.     static class BeginLineAction extends TextAction {
  1501.  
  1502.         /** 
  1503.          * Create this action with the appropriate identifier. 
  1504.          * @param nm  the name of the action, Action.NAME.
  1505.          * @param select whether to extend the selection when
  1506.          *  changing the caret position.
  1507.          */
  1508.         BeginLineAction(String nm, boolean select) {
  1509.             super(nm);
  1510.             this.select = select;
  1511.         }
  1512.  
  1513.         /** The operation to perform when this action is triggered. */
  1514.         public void actionPerformed(ActionEvent e) {
  1515.             JTextComponent target = getTextComponent(e);
  1516.             if (target != null) {
  1517.                 try {
  1518.                     int offs = target.getCaretPosition();
  1519.                     int begOffs = Utilities.getRowStart(target, offs);
  1520.                     if (select) {
  1521.                         target.moveCaretPosition(begOffs);
  1522.                     } else {
  1523.                         target.setCaretPosition(begOffs);
  1524.                     }
  1525.                 } catch (BadLocationException bl) {
  1526.                     target.getToolkit().beep();
  1527.                 }
  1528.             }
  1529.         }
  1530.  
  1531.         private boolean select;
  1532.     }
  1533.  
  1534.     /*
  1535.      * Position the caret to the end of the line.
  1536.      * @see DefaultEditorKit#endLineAction
  1537.      * @see DefaultEditorKit#selectEndLineAction
  1538.      * @see DefaultEditorKit#getActions
  1539.      */
  1540.     static class EndLineAction extends TextAction {
  1541.  
  1542.         /** 
  1543.          * Create this action with the appropriate identifier. 
  1544.          * @param nm  the name of the action, Action.NAME.
  1545.          * @param select whether to extend the selection when
  1546.          *  changing the caret position.
  1547.          */
  1548.         EndLineAction(String nm, boolean select) {
  1549.             super(nm);
  1550.             this.select = select;
  1551.         }
  1552.  
  1553.         /** The operation to perform when this action is triggered. */
  1554.         public void actionPerformed(ActionEvent e) {
  1555.             JTextComponent target = getTextComponent(e);
  1556.             if (target != null) {
  1557.                 try {
  1558.                     int offs = target.getCaretPosition();
  1559.                     int endOffs = Utilities.getRowEnd(target, offs);
  1560.                     if (select) {
  1561.                         target.moveCaretPosition(endOffs);
  1562.                     } else {
  1563.                         target.setCaretPosition(endOffs);
  1564.                     }
  1565.                 } catch (BadLocationException bl) {
  1566.                     target.getToolkit().beep();
  1567.                 }
  1568.             }
  1569.         }
  1570.  
  1571.         private boolean select;
  1572.     }
  1573.  
  1574.     /*
  1575.      * Position the caret to the beginning of the paragraph.
  1576.      * @see DefaultEditorKit#beginParagraphAction
  1577.      * @see DefaultEditorKit#selectBeginParagraphAction
  1578.      * @see DefaultEditorKit#getActions
  1579.      */
  1580.     static class BeginParagraphAction extends TextAction {
  1581.  
  1582.         /** 
  1583.          * Create this action with the appropriate identifier. 
  1584.          * @param nm  the name of the action, Action.NAME.
  1585.          * @param select whether to extend the selection when
  1586.          *  changing the caret position.
  1587.          */
  1588.         BeginParagraphAction(String nm, boolean select) {
  1589.             super(nm);
  1590.             this.select = select;
  1591.         }
  1592.  
  1593.         /** The operation to perform when this action is triggered. */
  1594.         public void actionPerformed(ActionEvent e) {
  1595.             JTextComponent target = getTextComponent(e);
  1596.             if (target != null) {
  1597.                 int offs = target.getCaretPosition();
  1598.                 Element elem = Utilities.getParagraphElement(target, offs);
  1599.                 offs = elem.getStartOffset();
  1600.                 if (select) {
  1601.                     target.moveCaretPosition(offs);
  1602.                 } else {
  1603.                     target.setCaretPosition(offs);
  1604.                 }
  1605.             }
  1606.         }
  1607.  
  1608.         private boolean select;
  1609.     }
  1610.  
  1611.     /*
  1612.      * Position the caret to the end of the paragraph.
  1613.      * @see DefaultEditorKit#endParagraphAction
  1614.      * @see DefaultEditorKit#selectEndParagraphAction
  1615.      * @see DefaultEditorKit#getActions
  1616.      */
  1617.     static class EndParagraphAction extends TextAction {
  1618.  
  1619.         /** 
  1620.          * Create this action with the appropriate identifier. 
  1621.          * @param nm  the name of the action, Action.NAME.
  1622.          * @param select whether to extend the selection when
  1623.          *  changing the caret position.
  1624.          */
  1625.         EndParagraphAction(String nm, boolean select) {
  1626.             super(nm);
  1627.             this.select = select;
  1628.         }
  1629.  
  1630.         /** The operation to perform when this action is triggered. */
  1631.         public void actionPerformed(ActionEvent e) {
  1632.             JTextComponent target = getTextComponent(e);
  1633.             if (target != null) {
  1634.                 int offs = target.getCaretPosition();
  1635.                 Element elem = Utilities.getParagraphElement(target, offs);
  1636.                 offs = elem.getEndOffset();
  1637.                 if (select) {
  1638.                     target.moveCaretPosition(offs);
  1639.                 } else {
  1640.                     target.setCaretPosition(offs);
  1641.                 }
  1642.             }
  1643.         }
  1644.  
  1645.         private boolean select;
  1646.     }
  1647.  
  1648.     /*
  1649.      * Move the caret to the begining of the document.
  1650.      * @see DefaultEditorKit#beginAction
  1651.      * @see DefaultEditorKit#getActions
  1652.      */
  1653.     static class BeginAction extends TextAction {
  1654.  
  1655.         /* Create this object with the appropriate identifier. */
  1656.         BeginAction(String nm, boolean select) {
  1657.             super(nm);
  1658.             this.select = select;
  1659.         }
  1660.  
  1661.         /** The operation to perform when this action is triggered. */
  1662.         public void actionPerformed(ActionEvent e) {
  1663.             JTextComponent target = getTextComponent(e);
  1664.             if (target != null) {
  1665.                 if (select) {
  1666.                     target.moveCaretPosition(0);
  1667.                 } else {
  1668.                     target.setCaretPosition(0);
  1669.                 }
  1670.             }
  1671.         }
  1672.  
  1673.         private boolean select;
  1674.     }
  1675.  
  1676.     /*
  1677.      * Move the caret to the end of the document.
  1678.      * @see DefaultEditorKit#endAction
  1679.      * @see DefaultEditorKit#getActions
  1680.      */
  1681.     static class EndAction extends TextAction {
  1682.  
  1683.         /* Create this object with the appropriate identifier. */
  1684.         EndAction(String nm, boolean select) {
  1685.             super(nm);
  1686.             this.select = select;
  1687.         }
  1688.  
  1689.         /** The operation to perform when this action is triggered. */
  1690.         public void actionPerformed(ActionEvent e) {
  1691.             JTextComponent target = getTextComponent(e);
  1692.             if (target != null) {
  1693.                 Document doc = target.getDocument();
  1694.                 int dot = doc.getLength();
  1695.                 if (select) {
  1696.                     target.moveCaretPosition(dot);
  1697.                 } else {
  1698.                     target.setCaretPosition(dot);
  1699.                 }
  1700.             }
  1701.         }
  1702.  
  1703.         private boolean select;
  1704.     }
  1705.  
  1706.     /*
  1707.      * Select the word around the caret
  1708.      * @see DefaultEditorKit#endAction
  1709.      * @see DefaultEditorKit#getActions
  1710.      */
  1711.     static class SelectWordAction extends TextAction {
  1712.  
  1713.         /** 
  1714.          * Create this action with the appropriate identifier. 
  1715.          * @param nm  the name of the action, Action.NAME.
  1716.          * @param select whether to extend the selection when
  1717.          *  changing the caret position.
  1718.          */
  1719.         SelectWordAction() {
  1720.             super(selectWordAction);
  1721.             start = new BeginWordAction("pigdog", false);
  1722.             end = new EndWordAction("pigdog", true);
  1723.         }
  1724.  
  1725.         /** The operation to perform when this action is triggered. */
  1726.         public void actionPerformed(ActionEvent e) {
  1727.             start.actionPerformed(e);
  1728.             end.actionPerformed(e);
  1729.         }
  1730.  
  1731.         private Action start;
  1732.         private Action end;
  1733.     }
  1734.  
  1735.     /*
  1736.      * Select the line around the caret
  1737.      * @see DefaultEditorKit#endAction
  1738.      * @see DefaultEditorKit#getActions
  1739.      */
  1740.     static class SelectLineAction extends TextAction {
  1741.  
  1742.         /** 
  1743.          * Create this action with the appropriate identifier. 
  1744.          * @param nm  the name of the action, Action.NAME.
  1745.          * @param select whether to extend the selection when
  1746.          *  changing the caret position.
  1747.          */
  1748.         SelectLineAction() {
  1749.             super(selectLineAction);
  1750.             start = new BeginLineAction("pigdog", false);
  1751.             end = new EndLineAction("pigdog", true);
  1752.         }
  1753.  
  1754.         /** The operation to perform when this action is triggered. */
  1755.         public void actionPerformed(ActionEvent e) {
  1756.             start.actionPerformed(e);
  1757.             end.actionPerformed(e);
  1758.         }
  1759.  
  1760.         private Action start;
  1761.         private Action end;
  1762.     }
  1763.  
  1764.     /*
  1765.      * Select the paragraph around the caret
  1766.      * @see DefaultEditorKit#endAction
  1767.      * @see DefaultEditorKit#getActions
  1768.      */
  1769.     static class SelectParagraphAction extends TextAction {
  1770.  
  1771.         /** 
  1772.          * Create this action with the appropriate identifier. 
  1773.          * @param nm  the name of the action, Action.NAME.
  1774.          * @param select whether to extend the selection when
  1775.          *  changing the caret position.
  1776.          */
  1777.         SelectParagraphAction() {
  1778.             super(selectParagraphAction);
  1779.             start = new BeginParagraphAction("pigdog", false);
  1780.             end = new EndParagraphAction("pigdog", true);
  1781.         }
  1782.  
  1783.         /** The operation to perform when this action is triggered. */
  1784.         public void actionPerformed(ActionEvent e) {
  1785.             start.actionPerformed(e);
  1786.             end.actionPerformed(e);
  1787.         }
  1788.  
  1789.         private Action start;
  1790.         private Action end;
  1791.     }
  1792.  
  1793.     /*
  1794.      * Select the entire document
  1795.      * @see DefaultEditorKit#endAction
  1796.      * @see DefaultEditorKit#getActions
  1797.      */
  1798.     static class SelectAllAction extends TextAction {
  1799.  
  1800.         /** 
  1801.          * Create this action with the appropriate identifier. 
  1802.          * @param nm  the name of the action, Action.NAME.
  1803.          * @param select whether to extend the selection when
  1804.          *  changing the caret position.
  1805.          */
  1806.         SelectAllAction() {
  1807.             super(selectAllAction);
  1808.         }
  1809.  
  1810.         /** The operation to perform when this action is triggered. */
  1811.         public void actionPerformed(ActionEvent e) {
  1812.             JTextComponent target = getTextComponent(e);
  1813.             if (target != null) {
  1814.                 Document doc = target.getDocument();
  1815.                 target.setCaretPosition(0);
  1816.                 target.moveCaretPosition(doc.getLength());
  1817.             }
  1818.         }
  1819.  
  1820.     }
  1821.  
  1822. }
  1823.